import {
	BitSet,
	BitClr,
	Set2Byte,
	DataGet,
	ab2hex,
	hexCharCodeToStr,
	arrayToBuffer,
	Parse,
	Set3Byte,
	bitToStr,
	Set1Byte,
	feedback,
	Set4Byte,
	Get2Byte,
	Get4Byte,
	Get3Byte,
	getByBite,uploadEquipCommlog,
} from "@/utils/index.js";
import {
	serviceId,
	characteristicId5,
} from "@/utils/service.js";

import {
	READ_AND_WRITE_STATUS,
} from "@/common/config.js";
export default {
	data() {
		return {
			startTime: null, //开始时间戳
		}
	},
	methods: {
		isRead() {
			let that = this;
			uni.showModal({
				title: '提示',
				content: '确定要查询吗？',
				success(res) {
					if (res.confirm) {
						that.doRead();
					}
				}
			})
		},
		// 读取设备信息
		async doRead(addr) {
			let that = this;
			that.$Toast.loading(`读取中...`);
			this.startTime = new Date().getTime();

			let datas = DataGet(this.command, 4, addr ? addr : 0, 0, 0, null);
			let buffer = arrayToBuffer(datas);
			let requestHex = ab2hex(buffer);
			
			let {
				deviceId,
			} = this.deviceInfo;
			uni.writeBLECharacteristicValue({
				deviceId,
				serviceId,
				characteristicId: characteristicId5,
				// 这里的value是ArrayBuffer类型
				value: buffer,
				success(res) {
					setTimeout(()=>{
						that.doCharData(addr);
					},300)
					
					uploadEquipCommlog(requestHex, '', READ_AND_WRITE_STATUS[1], 1, that.startTime,
						that.deviceInfo,that.dgMessage);
				},
				fail(err) {
					console.log(err)
					that.$Toast.none(`${that.dgMessage}读取失败!`);
					
					uploadEquipCommlog(requestHex, '', READ_AND_WRITE_STATUS[1], 0,that.startTime,
						that.deviceInfo,that.dgMessage);
				}
			})
		},
		// 读取数据
		doCharData(addr) {
			let {
				deviceId,
			} = this.deviceInfo;
			let that = this;
			
			this.startTime = new Date().getTime();
			
			// 读取低功耗蓝牙设备的特征值的二进制数据值
			uni.readBLECharacteristicValue({
				deviceId,
				serviceId,
				characteristicId: characteristicId5,
				success(re) {
					uni.onBLECharacteristicValueChange(function(res) {
						uni.hideLoading();
						// 解析数据
						const uint8Array = new Uint8Array(res.value);
						const newArray = Array.from(uint8Array);
						
						let databuff = Parse(newArray);
						
						uploadEquipCommlog('', ab2hex(res.value), READ_AND_WRITE_STATUS[0], 1,that.startTime,
						that.deviceInfo,that.dgMessage);
						
						// let value = res.value ? ab2hex(res.value) : '';
						if (databuff.databuff && databuff.databuff.length == 5) {
							for (let i = 0; i < databuff.databuff.length; i++) {
								that.versionInfo[`param${i+1}`] = databuff.databuff[i];
							}
						}
					})

				},
				fail(fal) {
					console.log("读取数据数据失败", fal)
					that.$Toast.none(`${that.dgMessage}读取失败!`);
					
					uploadEquipCommlog('', fal.errCode, READ_AND_WRITE_STATUS[0], 0,that.startTime,
						that.deviceInfo,that.dgMessage);
				}
			})
		},
		// 仓状态详情
		binPopup(index, command) {
			this.$refs.binpopup.open('center');
			this.currentIndex = index + 1;
			switch (command) {
				case 1: //检测单元
					this.dgMessage = `检测单元${this.currentIndex}`;
					break;
				case 2: //仓控单元
					this.dgMessage = `${this.currentIndex}号仓`;
					break;
				case 3: //柜控电源单元
					this.dgMessage = `柜控电源${this.currentIndex}`;
					break;
				case 4: //整流电源单元
					this.dgMessage = `整流电源${this.currentIndex}`;
					break;
				case 5: //电池单元
					this.dgMessage = `电池${this.currentIndex}`;
					break;
				case 7: //充电电源
					this.dgMessage = `充电电源${this.currentIndex}`;
					break;
			}
			for (let key of Object.keys(this.versionInfo)) {
				this.versionInfo[key] = "";
			}
			// 读取仓状态
			this.doRead(index);
		},
		closeBinpopup(pop) {
			this.$refs[pop].close();
		},
	},
}